//
//  WPMineFeedbackVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/6.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMineFeedbackVC.h"
#import "WPMinePlaceholderTextView.h"

@interface WPMineFeedbackVC ()<UITextViewDelegate>


@property (weak, nonatomic) IBOutlet UIView *feenBackFatherView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerImageHeightConstraint;
@property (strong, nonatomic)WPMinePlaceholderTextView *feedBackTextView;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;

@end

@implementation WPMineFeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat height = SCREEN_WIDTH*225/375;
    self.headerImageHeightConstraint.constant = height;
    
    [self.feenBackFatherView addSubview:self.feedBackTextView];
    [self.feedBackTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(38);
        make.top.equalTo(self.feenBackFatherView).offset(3);
        make.right.bottom.equalTo(self.feenBackFatherView);
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

- (IBAction)commitBtnDidClick:(UIButton *)sender
{
    if (!self.feedBackTextView.text.length)
    {
        [self showErrorInfoWithStatus:@"请输入反馈内容"];
        return ;
    }
    
    NSString *content = [NSString stringWithFormat:@"%@-%@",self.feedBackTextView.text,self.phoneField.text];
    NSString *url = @"user/feedback.do";
    NSDictionary *dict = @{@"suggestion":content};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            [self showSuccessInfoWithStatus:responseObject[@"detail"]];
            [self back];
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"反馈失败"];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

- (IBAction)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(WPMinePlaceholderTextView *)feedBackTextView
{
    if (_feedBackTextView == nil)
    {
        _feedBackTextView = [[WPMinePlaceholderTextView alloc] init];
        _feedBackTextView.font = [UIFont systemFontOfSize:14];
        _feedBackTextView.placeholder = @"写下您宝贵的建议和意见";
    }
    return _feedBackTextView;
}

@end
